// plugin/pages/hello-page.js
Page({
  data: {
    latitude: "",
    longitude: "",
    navigationBarHeight: 0,
    hotList: [],
    locationProvince: ""
  },
  onLoad(options) {
    console.log(options)
    if(options.token){
      wx.setStorageSync('token', options.token);
    }
    if(options.sign){
      wx.setStorageSync('sign', options.sign);
    }
    let that = this
    that.getCurrentLocation()
    wx.getSystemInfo({

      success: function (res) {
        // res.statusBarHeight 是状态栏的高度
        // 通常导航栏的高度是44px，可以根据实际情况调整
        const navigationBarHeight = res.statusBarHeight;
        that.setData({
          'navigationBarHeight': navigationBarHeight
        });
      },
      fail: function (data) {
        console.log(data)
      }
    });
  },
  getIndexData: function () {
    wx.showLoading({
      title: '请稍等',
      mask: true
    })
    let that = this;
    wx.request({
      url: 'https://zqey.zhkg66.cn/addons/yzy/api.Scenic/sceniclist',
      method: "GET",
      data: {
        page: 1,
        category_id: 1,
        search: '',
        sign: wx.getStorageSync('sign')
      },
      header: {
        appid: 'wxb5484226cb729d09'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            'hotList': res.data.data.data
          });
        }
      },
      fail: function () {
        wx.hideLoading()
      }
    })
  },

  gotoInfoPage: function () {

    wx.navigateTo({
      url: 'plugin://hello-plugin/info',
    })
  },

  gotoTravlPage: function () {
    wx.navigateTo({
      url: 'plugin://hello-plugin/travl',
    })
  },
  gotoLineDetail: function (e) {

    wx.navigateTo({
      url: 'plugin://hello-plugin/line-detail?id=' + e.currentTarget.dataset.item.id,
    })
  },
  gotoLinePage: function () {

    wx.navigateTo({
      url: 'plugin://hello-plugin/line',
    })
  },
  gotocard: function () {
    wx.navigateTo({
      url: 'plugin://hello-plugin/card',
    })
  },
  getCurrentLocation: function () {
    let that = this;
    wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res)
          const latitude = res.latitude; // 纬度
          const longitude = res.longitude; // 经度
          that.latitude = latitude;
          that.longitude = longitude;
          that.getIndexData();
          wx.request({
            url: 'https://api.map.baidu.com/geocoder/v2/?ak=Gvw6ZlATEWyfSW8Bhm7vz8eqzBmGcuRb&location=' +
              latitude + ',' + longitude + '&output=json',
            success: function (res) {
              if (res.data.status === 0) {
                const province = res.data.result.addressComponent
                  .province; // 获取城市名

                that.setData({
                  'locationProvince':province
                })
                
              } else {
                console.log('获取位置失败');
              }
            },
            fail: function (err) {
              console.log('请求失败：', err);
            }
          });
        
        
      },
      fail: function (err) {
        console.log('获取位置失败：', err);
      }
    });
},
})